// pages/subscribe/subscribe.js
import { Subscribe } from '../subscribe/subscribe-model.js';
import { My } from '../my/my-model.js';
import { Follow } from '../follow/follow-model.js';


var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
var demo = new QQMapWX({
  key: 'JCQBZ-DZQ6P-ZJEDL-LROIS-3K6Y2-3NF6R' // 必填
});
var base64 = require("../../utils/base64");
var sliderWidth = 60;

var my = new My();
var subscribe = new Subscribe();
var follow = new Follow();

Page({

  data: {
    loadingHidden: false,
     
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadData();
  },


  /**
 


 


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this._loadData(() => {
      wx.stopPullDownRefresh()
    });
  },
  mapS: function (e) {
    var that = this;
    var longitude;
    var latitude;
    demo.geocoder({
      address: e.currentTarget.dataset.name,
      success: function (ress) {
        longitude = ress.result.location.lng;
        latitude = ress.result.location.lat;
        wx.getLocation({
          // type: 'wgs84', //返回可以用于wx.openLocation的经纬度  
          success: function (res) {

            longitude: longitude;
            latitude: latitude;
            wx.openLocation({
              latitude: latitude,
              longitude: longitude,
              name: e.currentTarget.dataset.name,
              scale: 28
            })
          }
        })
        //  markers:
        // console.log(res);
        // that.setData({
        //     longitude:res.result.location.lng,
        //     latitude:res.result.location.lat,
        // //     markers: [{
        // //  // iconPath: "../../imgs/icon/map.png",
        // //   id: 0,
        // //   latitude: res.result.location.lat,
        // //   longitude: res.result.location.lng,
        // //   width: 35,
        // //   height: 45
        // // }]
        //   })
        // console.log(this.data.longitude);
      },

    });


    // console.log(e.currentTarget.dataset.name);
    // var longitude;
    // var latitude;
    // var markers;
    // console.log(options.cityName);

  },




  //加载所有数据
  _loadData: function () {
    var that = this;


    //  this.data.tabs[0].num = 
    my.getUserInfo((data) => {
      // console.log(data);
      that.setData({
        userInfo: data,
        loadingHidden: true
      });

    });
    wx.getSetting({
      success: (ress) => {

        // console.log(ress.authSetting['scope.userInfo']);
        if (!ress.authSetting['scope.userInfo']) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userInfo'] == true) {
                this._loadData();
                wx.switchTab({
                  url: '../my/my'
                });
              }
            }

          })
        } else {

          wx.getUserInfo({
            success: function (res) {
              // console.log(res);
              var userInfo = res.userInfo
              var nickName = userInfo.nickName
              var avatarUrl = userInfo.avatarUrl
              var gender = userInfo.gender //性别 0：未知、1：男、2：女
              var province = userInfo.province
              var city = userInfo.city
              var country = userInfo.country
            }
          })

          subscribe.getSubscribeAll((data) => {
            console.log(data);
            var len = 0;
            if (data == 0) {
              len = 1000
            } else {
              len = data.length * 1000
            }

            that.setData({
              storeTypeArr: data,
              loadingHidden: true,
              // 预约数量
              subscribeNum: data.length,
              sColor: true,
              fColor: false,
              lenHeight: len
            });

          });
      
          //查询是否生成了二维码
          // my.selQrcodeMode((data) => {


          //   that.setData({
          //     qrData: data
          //   })


          //   // console.log(data);
          // })
        }
      }
    })

    wx.getSystemInfo({

      success: function (res) {
        // console.log(res);
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
    // this._getOrders();
    // order.execSetStorageSync(false);  //更新标志位
  },
  //支付定金
  pay: function (e) {
    var id = subscribe.getDataSet(e, 'id');
    var that = this;
    my.paySub(id, (data) => {
      //  console.log(data);
      var timeStamp = data.timeStamp;
      var nonceStr = data.nonceStr;
      var p_package = data.package;
      var paySign = data.paySign;
      var signType = data.signType
      // console.log(timeStamp + '+' + nonceStr + '+' + p_package + '+' + paySign);
      wx.requestPayment(
        {
          // 'appId': data.appid,
          'timeStamp': timeStamp,
          'nonceStr': nonceStr,
          'package': p_package,
          'signType': signType,
          'paySign': paySign,
          'success': function (res) {
            that._loadData();
          },
          'fail': function (res) {
            // console.log(res)
          },
          'complete': function (res) {
            // console.log(res)
          }
        })
      // that.setData({

      // });

    });
  },
  //去逛逛
  detail: function (event) {

    wx.switchTab({
      url: '../category/category'
    }); 
  },
  /*跳转到商品详情*/
  onProductsItemTap: function (event) {
    var id = subscribe.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },



})